<?php

namespace app\company\service;

use app\company\model\CompanyPositionModel;
use app\exception\MsgException;
use app\model\RoleModel;
use think\db\exception\DbException;
use think\facade\Db;
use think\Paginator;

class PositionService extends Base
{

    /**
     * 获取表格数据
     * @param array $params
     * @return Paginator
     * @throws DbException
     */
    public function getList(array $params): Paginator
    {
        $pageSize = $params['pageSize']?? 10;

        $model = new CompanyPositionModel();

        if(!empty($params['cid'])){
            $model = $model->where('cid', $params['cid']);
        }
        if(!empty($params['name'])){
            $model = $model->where('name', 'like', "%{$params['name']}%");
        }

        return $model->order('sort', 'desc')
            ->paginate($pageSize);
    }

    /**
     * 新增
     * @param array $data
     * @return CompanyPositionModel
     * @throws MsgException
     */
    public function add(array $data): CompanyPositionModel
    {
        $model = new CompanyPositionModel();

        try{
            Db::startTrans();

            //新增对应的角色
            $roleModel = new RoleModel();
            $roleModel->save([
                'name' => $data['name']??'',
                'app_id' => $this->getAppid(),
                'remark' => '公司职位-系统自动创建',
                'is_auto' => 1 //标记为系统自动创建的
            ]);

            //补充职位ID
            $data['role_id'] = $roleModel->id;

            //新增职位
            $res = $model::create($data);

            Db::commit();
            return $res;
        }catch (\Throwable $e){
            Db::rollback();
            throw new MsgException("新增失败，请重试: ". $e->getMessage());
        }

    }

    /**
     * 编辑
     * @param array $data
     * @return CompanyPositionModel
     * @throws DbException
     * @throws MsgException
     */
    public function edit(array $data): CompanyPositionModel
    {
        $model = CompanyPositionModel::where('id', $data['id'])->find();

        try{
            Db::startTrans();

            //编辑角色权限
            if(isset($data['rules'])){
                RoleModel::where('id', $model->role_id)
                    ->save([
                        'rules' => $data['rules'],
                        'rules_checked' => $data['rules_checked'],
                    ]);
                unset($data['rules']);
                unset($data['rules_checked']);
            }
            $model->save($data);

            Db::commit();
            return $model;
        }catch (\Throwable $e){
            Db::rollback();
            throw new MsgException("新增失败，请重试: ". $e->getMessage());
        }
    }

    /**
     * 批量删除
     * @param $ids
     * @return bool
     * @throws DbException
     */
    public function delete($ids): bool
    {
        $list = CompanyPositionModel::whereIn('id', $ids)->select();
        foreach ($list as $item) {
            RoleModel::where('id', $item->role_id)->delete();
            $item->delete();
        }

        return true;
    }

    /**
     * 获取当前应用的权限树
     * @return array
     */
    public function getRuleTree(): array
    {
        $rbacService = new \plugin\admin\app\service\AdminRbacService();
        return $rbacService->getRuleList([
            'app_id' => $this->getAppid()
        ]);
    }
}